package ${packageName}.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
<#if loginModel == "动态用户">
import org.springframework.beans.factory.annotation.Autowired;
</#if>
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import ${packageName}.entity.User;
<#if loginModel == "动态用户">
import ${packageName}.service.IUserService;
</#if>

@RestController
@CrossOrigin("*")
@RequestMapping("/login")
public class UserController <#if loginModel == "动态用户"> extends BaseController </#if> {

<#if loginModel == "动态用户">
	private IUserService service;

	@Autowired
	public UserController(IUserService service) {
		super(service);
		this.service = service;
	}
	
	@RequestMapping("/doLogin")
	public Map<String, Object> doLogin(@RequestBody User user, HttpSession session) {

		Map<String, Object> resultMap = new HashMap<>();

		User currentUser = service.login(user);
		if (currentUser != null) {
			session.setAttribute("user", user);
			resultMap.put("result", "success");
			return resultMap;
		}

		resultMap.put("result", "failed");
		return resultMap;

	}
</#if>
	
<#if loginModel == "静态用户">
	
	@RequestMapping("/doLogin")
	public Map<String, Object> doLogin(@RequestBody User user, HttpSession session) {

		Map<String, Object> resultMap = new HashMap<>();

		if (${userCondition}) {

			session.setAttribute("user", user);
			resultMap.put("result", "success");
			return resultMap;
		}
		resultMap.put("result", "failed");
		return resultMap;

	}
</#if>
	
	@RequestMapping("/doLogOut")
	public void doLogOut(HttpSession session) {

		session.removeAttribute("user");

	}

}
